package com.js.lib_common.mvp.presenter;

import android.util.Log;

import com.js.lib_common.base.BasePresenter;
import com.js.lib_common.http.ApiCallback;
import com.js.lib_common.mvp.model.CommonBean;
import com.js.lib_common.mvp.model.PageBean;
import com.js.lib_common.mvp.model.response.MissionResponse;
import com.js.lib_common.mvp.view.MissionListView;

import java.util.List;

import io.reactivex.functions.Predicate;

import static com.js.lib_common.http.ResponseCode.HTTP_CODE_OK;

/**
 * 任务列表
 * Created by wz on 2020/11/18.
 */
public class MissionListPresenter extends BasePresenter<MissionListView> {
public  static  final String status_done="100";//-1查询所有未完成的工单
    public  static  final String status_undone="-1";//100-任务完成归档



    public MissionListPresenter(MissionListView view) {
        attachView(view);
    }


    public void getMissionList(final int page ,String orderStatus) {

        addSubscription(apiStores.getMissionList(page, 10 , orderStatus)
                        .filter(new Predicate<CommonBean<List<MissionResponse>>>() {
                            @Override
                            public boolean test(CommonBean<List<MissionResponse>> listCommonBean)
                                    throws Exception {
                                if (listCommonBean.getCode() == HTTP_CODE_OK)
                                    return true;
                                else {
                                    mvpView.onFailed(String.format("%s", listCommonBean.getCode()));
                                    return false;
                                }

                            }
                        })

                 ,
                new ApiCallback<CommonBean<List<MissionResponse>>>() {


                    @Override
                    public void onSuccess(CommonBean<List<MissionResponse>> response) {
                        boolean hasMore = true;
                        if (  response.getCode()==0) {
                            PageBean pageBean=  response.getPage();
                            int pageNow=page;
                            if (pageBean!=null) {
                                Log.e("xxxx",pageBean.toString());
                                pageNow = pageBean.getCurrentPage();
                                if (pageBean.getCurrentPage() == pageBean.getTotalPage()) {
                                    hasMore = false;
                                }
                            }
                            mvpView.showMissionList(pageNow,hasMore,response.getList());

                        } else {
                            mvpView.onError(response.getMsg());
                        }

                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("getMissionList fail %s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }











}
